package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.wx.bean.bo.WxAddressSaveBo;
import com.cskaoyan.wx.bean.vo.WxAddressDetailVo;
import com.cskaoyan.wx.bean.vo.WxAddressListVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxAddressServicelmpl implements WxAddressService {

    private static int userId;

    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    UserMapper userMapper;

    private void usernameToId() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        //根据username获取用户其他信息
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();
        userExampleCriteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        userId = users.get(0).getId();
    }

    /**
     * 功能:地址,/list
     * 作者:whj
     */
    @Override
    public List<WxAddressListVo> list() {
        List<WxAddressListVo> wxAddressListVos = new ArrayList<>();
        usernameToId();
        //根据用户id查询地址信息
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        //根据区域id,拼成地址
        for (Address address : addresses) {
            WxAddressListVo wxAddressListVo = new WxAddressListVo();
            wxAddressListVo.setIsDefault(address.getIsDefault());
            wxAddressListVo.setName(address.getName());
            wxAddressListVo.setMobile(address.getMobile());
            wxAddressListVo.setId(address.getId());
            //根据区域id,查询地址信息
            Integer provinceId = address.getProvinceId();
            Region region1 = regionMapper.selectByPrimaryKey(provinceId);
            String province = region1.getName();

            Integer cityId = address.getCityId();
            Region region2 = regionMapper.selectByPrimaryKey(cityId);
            String city = region2.getName();

            Integer areaId = address.getAreaId();
            Region region3 = regionMapper.selectByPrimaryKey(areaId);
            String area = region3.getName();

            String detailedAddress = province + city + area + address.getAddress();
            wxAddressListVo.setDetailedAddress(detailedAddress);
            wxAddressListVos.add(wxAddressListVo);
        }
        return wxAddressListVos;
    }

    /**
     * 功能:地址,/detail
     * 作者:whj
     */
    @Override
    public WxAddressDetailVo detail(Integer id) {
        WxAddressDetailVo wxAddressDetailVo = new WxAddressDetailVo();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andDeletedEqualTo(false).andIdEqualTo(id);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        Address address = addresses.get(0);
        wxAddressDetailVo.setIsDefault(address.getIsDefault());
        wxAddressDetailVo.setAddress(address.getAddress());
        Integer areaId = address.getAreaId();
        Region region3 = regionMapper.selectByPrimaryKey(areaId);
        String area = region3.getName();
        wxAddressDetailVo.setAreaId(areaId);
        wxAddressDetailVo.setAreaName(area);

        Integer cityId = address.getCityId();
        Region region2 = regionMapper.selectByPrimaryKey(cityId);
        String city = region2.getName();
        wxAddressDetailVo.setCityId(cityId);
        wxAddressDetailVo.setCityName(city);

        Integer provinceId = address.getProvinceId();
        Region region1 = regionMapper.selectByPrimaryKey(provinceId);
        String province = region1.getName();
        wxAddressDetailVo.setProvinceId(provinceId);
        wxAddressDetailVo.setProvinceName(province);

        wxAddressDetailVo.setName(address.getName());
        wxAddressDetailVo.setMobile(address.getMobile());
        wxAddressDetailVo.setId(address.getId());
        return wxAddressDetailVo;
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public int delete(Integer id) {
        Address address = new Address();
        address.setId(id);
        address.setDeleted(true);
        int code = addressMapper.updateByPrimaryKeySelective(address);
        if(code == 0){
            return 404;
        }
        return 200;
    }

    /**
     * 功能:地址,/save
     * 作者:whj
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public int save(WxAddressSaveBo wxAddressSaveBo) {
        Address address = new Address();
        //如果添加时选了默认,把该用户其他默认置0
        usernameToId();
        if(wxAddressSaveBo.isIsDefault()){
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            address.setIsDefault(false);
            addressMapper.updateByExampleSelective(address,addressExample);
        }
        //更改
        if(wxAddressSaveBo.getId() != 0){
            address.setId(wxAddressSaveBo.getId());
            address.setName(wxAddressSaveBo.getName());
            address.setUserId(userId);
            address.setProvinceId(wxAddressSaveBo.getProvinceId());
            address.setCityId(wxAddressSaveBo.getCityId());
            address.setAreaId(wxAddressSaveBo.getAreaId());
            address.setAddress(wxAddressSaveBo.getAddress());
            address.setMobile(wxAddressSaveBo.getMobile());
            address.setIsDefault(wxAddressSaveBo.isIsDefault());
            address.setAddTime(new Date(System.currentTimeMillis()));
            address.setUpdateTime(new Date(System.currentTimeMillis()));
            address.setDeleted(false);

            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andUserIdEqualTo(userId).andIdEqualTo(wxAddressSaveBo.getId());
            int code = addressMapper.updateByExampleSelective(address, addressExample);
            if(code == 0){
                return 404;
            }
            return wxAddressSaveBo.getId();
        }
        //新增,插入数据库
        address.setId(null);
        address.setName(wxAddressSaveBo.getName());
        address.setUserId(userId);
        address.setProvinceId(wxAddressSaveBo.getProvinceId());
        address.setCityId(wxAddressSaveBo.getCityId());
        address.setAreaId(wxAddressSaveBo.getAreaId());
        address.setAddress(wxAddressSaveBo.getAddress());
        address.setMobile(wxAddressSaveBo.getMobile());
        address.setIsDefault(wxAddressSaveBo.isIsDefault());
        address.setAddTime(new Date(System.currentTimeMillis()));
        address.setUpdateTime(new Date(System.currentTimeMillis()));
        address.setDeleted(false);
        int code = addressMapper.insertBackId(address);
        if(code == 0){
            return 404;
        }
        return address.getId();
    }
}
